package SentenceTranslator;

import Main.SymbolManager;
import Include.TACLine;
import Include.ASMConstructor;
import Common.REG;
import Common.TacOperandType;
import Common.POSTYPE;

import java.util.*;

public class IfTranslator {
    public List<String> asmlines;
    public List<String> SentenceTranslate(SymbolManager SymbolManager_, TACLine TACLine_) {
        asmlines = new ArrayList<String>();
        String str_op = TACLine_.op.commonop2String();

        String str_label = TACLine_.dst.value;
        String str_src1 = TACLine_.src1.value;
        String str_src2 = TACLine_.src2.value;
        REG reg_src1 = REG.Cnts;
        if (TACLine_.src1.operType == TacOperandType.IMM) {
            reg_src1 = REG.EDI;
            asmlines.add(ASMConstructor.construct_asm("mov", reg_src1, str_src1));
        } else {
            String str_src1_encode = SymbolManager_.encodeVar(str_src1);
            POSTYPE pos = SymbolManager_.position(str_src1_encode);
            if (pos == POSTYPE.REG) {
                reg_src1 = SymbolManager_.avalueReg(str_src1_encode);
            } else if (pos == POSTYPE.MEM) {
                int mem_src1 = SymbolManager_.avalueMem(str_src1_encode);
                reg_src1 = REG.EDI;
                asmlines.add(ASMConstructor.construct_asm("mov", reg_src1, mem_src1));
            } else if (pos == POSTYPE.GLOBAL) {
                reg_src1 = REG.EDI;
                asmlines.add(ASMConstructor.construct_asm("mov", reg_src1, str_src1));
            } else {
                System.out.println("if src1 default error");
            }
        }

        if (TACLine_.src2.operType == TacOperandType.IMM) {
            asmlines.add(ASMConstructor.construct_asm("cmp", reg_src1, str_src2));
        } else if (TACLine_.src2.operType == TacOperandType.VAR) {
            String str_src2_encode = SymbolManager_.encodeVar(str_src2);
            POSTYPE pos = SymbolManager_.position(str_src2_encode);
            if (pos == POSTYPE.REG) {
                REG reg_src2 = SymbolManager_.avalueReg(str_src2_encode);
                asmlines.add(ASMConstructor.construct_asm("cmp", reg_src1, reg_src2));
            } else if (pos == POSTYPE.MEM) {
                int mem_src2 = SymbolManager_.avalueMem(str_src2_encode);
                asmlines.add(ASMConstructor.construct_asm("cmp", reg_src1, mem_src2));
            } else if (pos == POSTYPE.GLOBAL) {
                asmlines.add(ASMConstructor.construct_asm("cmp", reg_src1, str_src2));
            } else {
                System.out.println("if src2 default error");
            }
        }
        asmlines.add(ASMConstructor.construct_asm(str_op, str_label));
        return asmlines;
    }
}